/**
 * Notes: 兼职模块业务逻辑
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2024-03-23 04:00:00 
 */

const BaseProjectService = require('./base_project_service.js');
const dataUtil = require('../../../framework/utils/data_util.js');
const timeUtil = require('../../../framework/utils/time_util.js');
const util = require('../../../framework/utils/util.js');
const cloudUtil = require('../../../framework/cloud/cloud_util.js');
const JobModel = require('../model/job_model.js');
const UserModel = require('../model/user_model.js');

class JobService extends BaseProjectService {

	/** 点赞 */
	async likeJob(userId, id) {
		try {
      // 获取留言详情
      let job = await this.getJobDetail(id);
      if (!job) {
          return {
              code: -1,
              message: '点赞信息不存在',
              data: null
          };
      }
      
      // 获取当前点赞列表
      let arr = job['JOB_LIKE_LIST'];

      // 定义一个标志位，用于表示是否已经点赞
      let flag = false;

      // 检查用户是否已经在点赞列表中
      if (arr.includes(userId)) {
          // 如果用户已经点赞了，则取消点赞
          arr = arr.filter(item => item != userId);
          flag = false; // 表示取消点赞
      } else {
          // 如果用户未点赞，则执行点赞操作
          arr.push(userId);
          flag = true; // 表示点赞
      }
      
      // 更新留言详情中的点赞列表和点赞数量
      await JobModel.edit(id, {
          ["JOB_LIKE_LIST"]: arr,
          ["JOB_LIKE_CNT"]: arr.length
      });

      return {
        isLike:flag
      };
  } catch (err) {
      console.error('[点赞操作失败]', err);
      return {
          code: -1,
          message: '点赞操作失败：' + err.message,
          data: null
      };
  }
	}

	/** 浏览 */
	async viewJob(userId, id) {
		let fields = '*';

		let where = {
			_id: id,
			//JOB_STATUS: 1
		}

		if (userId && util.isDefined(where.JOB_STATUS)) delete where.JOB_STATUS;

		let job = await JobModel.getOne(where, fields);
		if (!job) return null;

		JobModel.inc(id, 'JOB_VIEW_CNT', 1);

		return job;
	}

	/** 获取 */
	async getJobDetail(id) {
		return await JobModel.getOne(id);
	}

	/** 修改状态 */
	async statusJob(userId, id, status) {
    try {
      // 获取失物招领详情
      let job = await this.getJobDetail(id);
      if (!job) {
          return {
              code: -1,
              message: '兼职工作不存在',
              data: null
          };
      }

      // 检查用户权限
      if (job.JOB_USER_ID !== userId) {
          return {
              code: -1,
              message: '您无权修改该兼职状态',
              data: null
          };
      }

      // 更新状态
      await jobModel.edit(id, { JOB_STATUS: status });

      return {
          code: 0,
          message: '修改兼职状态成功',
          data: null
      };
  } catch (err) {
      console.error('[修改兼职状态失败]', err);
      return {
          code: -1,
          message: '修改兼职状态失败：' + err.message,
          data: null
      };
  }

	}

	/** 删除 */
	async delJob(userId, id) {
    try {
      await JobModel.del(userId,id);
      return {
          code: 0,
          message: '删除兼职成功',
          data: null
      };
  } catch (err) {
      console.error('[删除兼职失败]', err);
      return {
          code: -1,
          message: '删除兼职失败：' + err.message,
          data: null
      };
  }
	}

	/** 插入 */
	async insertJob(userId, {
		cateId,
		cateName,
		order,
		forms
	}) {
    try {
      let data = {
        JOB_USER_ID : userId,
        JOB_CATE_ID : cateId,
        JOB_CATE_NAME : cateName,
        JOB_ORDER : order ,
        JOB_OBJ : dataUtil.dbForms2Obj(forms),
        JOB_FORMS : forms
      }
      await JobModel.insert(data);
        // 返回插入成功的结果
      return {
        code: 0,
        message: '插入兼职工作成功',
        data
      };
    }catch (err) {
        // 如果发生错误，可以捕获错误并进行处理
        console.error('[插入兼职工作失败]', err);
        // 返回错误信息
        return {
            code: -1,
            message: '插入兼职工作失败：' + err.message,
            data: null
        };
    }
	}

	/** 修改 */
	async editJob(userId, {
		id,
		cateId,
		cateName,
		order,
		forms
	}) {

    try {
      let data = {
        JOB_USER_ID : userId,
        JOB_CATE_ID : cateId,
        JOB_CATE_NAME : cateName,
        JOB_ORDER : order ,
        JOB_OBJ : dataUtil.dbForms2Obj(forms),
        JOB_FORMS : forms
      };

		this.AppError('[校园圈]该功能暂不开放，如有需要请加作者微信：cclinux0730');
      let result = await JobModel.edit(id,data);
      return {
          code: 0,
          message: '修改兼职工作成功',
          data: result
      };
  } catch (err) {
      console.error('[修改兼职工作失败]', err);
      return {
          code: -1,
          message: '修改兼职工作失败：' + err.message,
          data: null
      };
  }
	}

	/** 更新forms信息 */
	async updateJobForms({
		id,
		hasImageForms
	}) {
    try {
      const cloud = cloudBase.getCloud();
      const fileIDs = await Promise.all(hasImageForms.map(async image => {
          const res = await cloud.uploadFile({
              cloudPath: 'job_images/' + dataUtil.getUniqueFileName(image.name),
              fileContent: image.data,
          });
          return res.fileID;
      }));

      const result = await JobModel.edit(id, { JOB_IMAGES: fileIDs });
      return {
          code: 0,
          message: '更新兼职工作成功',
          data: result
      };
  } catch (err) {
      console.error('[更新兼职工作失败]', err);
      return {
          code: -1,
          message: '更新兼职工作失败：' + err.message,
          data: null
      };
  }
	}

	/** 列表与搜索 */
	async getJobList(userId, {
		search, // 搜索条件
		sortType, // 搜索菜单
		sortVal, // 搜索菜单
		orderBy, // 排序
		whereEx, //附加查询条件
		page,
		size,
		isTotal = true,
		oldTotal }) {
		orderBy = orderBy || {
			'JOB_ORDER': 'asc',
			'JOB_ADD_TIME': 'desc'
		};
		let fields = 'JOB_ORDER,JOB_CATE_ID,JOB_CATE_NAME,JOB_STATUS,JOB_COMMENT_CNT,JOB_VIEW_CNT,JOB_FAV_CNT,JOB_FAV_LIST,JOB_LIKE_CNT,JOB_LIKE_LIST,JOB_ADD_TIME,JOB_USER_ID,JOB_OBJ,user.USER_NAME,user.USER_PIC';

		let where = {};
		where.and = {
			//JOB_STATUS: 1,
			_pid: this.getProjectId() //复杂的查询在此处标注PID
		};


		if (util.isDefined(search) && search) {
			if (search == '我发布的职位') {
				where.and.JOB_USER_ID = userId;
			}
			else if (search == '我的点赞') {
				where.and.JOB_LIKE_LIST = userId;
			}
			else if (search == '我的收藏') {
				where.and.JOB_FAV_LIST = userId;
			}
			else {
				where.or = [
					{ 'JOB_CATE_NAME': ['like', search] },
					{ 'JOB_OBJ.title': ['like', search] },
					{ 'JOB_OBJ.company': ['like', search] },
					{ 'JOB_OBJ.address': ['like', search] },
					{ 'JOB_OBJ.desc': ['like', search] },
					{ 'JOB_OBJ.poster': ['like', search] },
					{ 'JOB_OBJ.tel': ['like', search] },
					{ 'JOB_OBJ.wx': ['like', search] },
				];
			}

		} else if (sortType && util.isDefined(sortVal)) {
			// 搜索菜单
			switch (sortType) {
				case 'cateId': {
					where.and.JOB_CATE_ID = String(sortVal);
					break;
				}
				case 'status': {
					where.and.JOB_STATUS = Number(sortVal);
					break;
				}
				case 'sort': {
					orderBy = this.fmtOrderBySort(sortVal, 'JOB_ADD_TIME');
					break;
				}
				case 'fav': {
					orderBy = {
						'JOB_FAV_CNT': 'desc',
						'JOB_ADD_TIME': 'desc'
					}
					break;
				}
				case 'comment': {
					orderBy = {
						'JOB_COMMENT_CNT': 'desc',
						'JOB_ADD_TIME': 'desc'
					}
					break;
				}
				case 'like': {
					orderBy = {
						'JOB_LIKE_CNT': 'desc',
						'JOB_ADD_TIME': 'desc'
					}
					break;
				}
				case 'today': {
					where.and.JOB_DAY = timeUtil.time('Y-M-D');
					break;
				}
				case 'yesterday': {
					where.and.JOB_DAY = timeUtil.time('Y-M-D', -86400);
					break;
				}
			}
		}

		let joinParams = {
			from: UserModel.CL,
			localField: 'JOB_USER_ID',
			foreignField: 'USER_MINI_OPENID',
			as: 'user',
		};

		return await JobModel.getListJoin(joinParams, where, fields, orderBy, page, size, isTotal, oldTotal);

	}

}

module.exports = JobService;